<template>
  <el-form>
    <el-form-item
      label="输入旧密码"
      required=""
    >
      <el-input v-model="modify.old_password" placeholder="请输入" show-password />
    </el-form-item>
    <el-form-item
      label="设置新密码"
      required=""
    >
      <el-input v-model="modify.new_password" placeholder="输入密码" show-password />
    </el-form-item>
    <el-form-item
      label="确认新密码"
      required=""
    >
      <el-input v-model="modify.ensure_password" placeholder="请确认密码" show-password />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">修改密码</el-button>
    </el-form-item>
  </el-form>
</template>

<script>

import { checkOldPassWord, modifyPassWord } from '@/api/user'

export default {
  data() {
    return {
      modify: {
        password: '',
        old_password: '',
        new_password: '',
        ensure_password: ''
      }
    }
  },
  methods: {
    submit() {
      if (this.modify.old_password !== '' && this.modify.new_password !== '' && this.modify.ensure_password !== '') {
        if (this.modify.new_password === this.modify.ensure_password) {
          this.modify.password = this.modify.new_password
          checkOldPassWord(this.modify.old_password).then(ck => {
            if (ck.code === '20000') {
              modifyPassWord(this.modify).then(res => {
                if (res.code === '20000') {
                  this.modify = {}
                  this.$message.success('密码更改成功!')
                }
              })
            } else if (ck.code === '30001') {
              this.$message.warning('旧密码填写错误!')
            } else {
              this.modify = {}
              this.$message.info('服务器异常, 请稍后再试!')
            }
          })
        } else {
          this.$message.warning('新密码填写不一致!')
        }
      } else {
        this.$message.warning('请将信息填写完整!!')
      }
    }
  }
}
</script>
